
#import "smListDetailControllerViewController.h"
#import "smListDetailPlanViewController.h"
#import "smPhotoItemView.h"
#import "GetPhotos.h"
#import "smMboSmTbProject.h"

@interface smListDetailControllerViewController ()<UIScrollViewDelegate,AddShopPhotoDelegate,UpLoadImageDelegate,UIActionSheetDelegate,GetPhotosDelegate>

@property(strong,nonatomic)smMboSmTbProject* projectDetail;
@property(strong,nonatomic)UIScrollView* myScrollView;
@property (nonatomic, strong) NSMutableArray* myOrderPhontos;
@property (nonatomic, strong) GetPhotos* getPhoto;

@end


@implementation smListDetailControllerViewController

@synthesize projectDetail=_projectDetail;
@synthesize myScrollView=_myScrollView;

@synthesize myOrderPhontos = _myOrderPhontos;
@synthesize getPhoto = _getPhoto;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.navigationController.navigationBar setHidden:YES];
    [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"project_background.png"]]];
    
    UIImageView* innerImageView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"project_title_manage.png"]];
    innerImageView.frame=CGRectMake(0, 0, 768, 70);
    
    //后退
    UIButton* backButton=[UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame=CGRectMake(5, 5, 118, 52);
    [backButton setImage:[UIImage imageNamed:@"back_button_up.png"] forState:UIControlStateNormal];
    [backButton setImage:[UIImage imageNamed:@"back_button_down.png"] forState:UIControlStateHighlighted];
    backButton.backgroundColor=[UIColor clearColor];
    [backButton addTarget:self action:@selector(backClick:) forControlEvents:UIControlEventTouchUpInside];

    
    //项目进度        
    UIButton* planButton=[UIButton buttonWithType:UIButtonTypeCustom];
    planButton.frame=CGRectMake(619, 5, 140, 52);
    [planButton setImage:[UIImage imageNamed:@"project_button_plan_up.png"] forState:UIControlStateNormal];
    [planButton setImage:[UIImage imageNamed:@"project_button_plan_down.png"] forState:UIControlStateHighlighted];
    planButton.backgroundColor=[UIColor clearColor];
    [planButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:innerImageView];
    [self.view addSubview:backButton];
    [self.view addSubview:planButton];   
    [self initEditMyOrderView];
    //大图浏览        
//    self.myScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(80, 50, 620, 720)];
//    self.myScrollView.contentSize = CGSizeMake(self.myScrollView.frame.size.width * 2, 
//                                               self.myScrollView.frame.size.height);
//    
//    self.myScrollView.delegate = self;
//    self.myScrollView.pagingEnabled = YES;
//    self.myScrollView.showsHorizontalScrollIndicator = YES;
//    
//    [self.view addSubview:self.myScrollView];
//    for (int i = 0; i < 2; i++) {
//        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"img%d.jpg",i]];
//        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
//        imageView.frame = CGRectMake(self.myScrollView.frame.size.width * i, 0, self.myScrollView.frame.size.width, self.myScrollView.frame.size.height);
//        [self.myScrollView addSubview:imageView];
//    }
   

    //项目描述
    UILabel*tempLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 600, 340, 50)];
    [tempLabel setTextColor:[UIColor whiteColor]];
    tempLabel.font=[UIFont systemFontOfSize:30];  
    [tempLabel setText:@"  项目介绍:"];
    [tempLabel setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"house_bar.png"]]];
    [self.view addSubview:tempLabel];
    
    UILabel*tempLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(10, 660, 750, 200)];
    tempLabel2.font=[UIFont systemFontOfSize:22];
    [tempLabel2 setTextColor:[UIColor colorWithRed:112/255.0 green:112/255.0 blue:112/255.0 alpha:1.0]];   
    tempLabel2.lineBreakMode = UILineBreakModeWordWrap; 
    tempLabel2.numberOfLines = 0;
    [tempLabel2 setText:[[NSString alloc] initWithFormat:@"        %@",self.projectDetail.projectDescription]];
    [tempLabel2 sizeToFit];
    [tempLabel2 setBackgroundColor:[UIColor clearColor]];
    [self.view addSubview:tempLabel2];
    
}



-(void) initEditMyOrderView {
    

    self.myScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 62, 758, 512)];
    [self.myScrollView setContentSize:CGSizeMake(512*5+10, 512)];
    [self.myScrollView setShowsHorizontalScrollIndicator:NO];
    [self.myScrollView setShowsVerticalScrollIndicator:NO];
    [self.myScrollView setScrollEnabled:YES];
    [self.myScrollView setBackgroundColor:[UIColor clearColor]];
    self.myOrderPhontos = [[NSMutableArray alloc] init];
    
    for (int i=0; i<5; i++) {
        
        smPhotoItemView* tempImg = [[smPhotoItemView alloc] initWithFrame:CGRectMake(512*i+10, 10, 650, 700)];
        [tempImg setShopDefaultImage: [UIImage imageNamed:[NSString stringWithFormat:@"loading.png",i]]];
        [self.myOrderPhontos addObject:tempImg];
        [tempImg setAddShopimgDelegate:self];
        [tempImg setUploadimgDelegate:self];
        tempImg.tag = i;
        [self.myScrollView addSubview:tempImg];
    }
    

    [self.view addSubview:self.myScrollView];
    
    //Set店铺图片
    NSString* otherAllImgStr = nil;
    
    //模拟数据
    //菊花
    otherAllImgStr = [NSString stringWithFormat:@"http://pica.nipic.com/2007-11-24/20071124161027627_2.jpg,http://pic1a.nipic.com/2008-09-18/2008918133423510_2.jpg"];
    
    NSLog(otherAllImgStr,nil);
    
    if (otherAllImgStr == nil) {
        return;
    }
    
    NSArray* tempOtherImgArray = [otherAllImgStr componentsSeparatedByString:@","];
    
    if (tempOtherImgArray == nil) {
        return;
    }
    
    int TempIndex = 0;
    
    for (NSString* otherImgStr in tempOtherImgArray) {
        if (TempIndex > 4) {
            return;
        }
        
        [[self.myOrderPhontos objectAtIndex:TempIndex] setmyShopHttpImgUrl:otherImgStr];
        TempIndex++;
    }
}


- (void)viewDidUnload
{
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(void) buttonClick:(UIButton*) bt {
    
    smListDetailPlanViewController* detailPlanViewController = [[smListDetailPlanViewController alloc] init];
    [detailPlanViewController getProjectDetail:self.projectDetail];
    [self.navigationController pushViewController:detailPlanViewController animated:YES];

}

-(void) backClick:(UIButton*) bt{
    
  [self.navigationController popViewControllerAnimated:YES];
}

-(smMboSmTbProject*)setSelectRowAtIndexObject:(smMboSmTbProject*)project{
    
    self.projectDetail=project;
    
    return self.projectDetail;
}


- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{    
     [[scrollView.subviews objectAtIndex:0] setZoomScale:0.5];
    return [scrollView.subviews objectAtIndex:0];
}


- (void) addShopPhoto:(id) sel {
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"亲，请选择照片来源"
															 delegate:self
                                                    cancelButtonTitle:@"取消，不选了"
                                               destructiveButtonTitle:@"照相机拍照"
                                                    otherButtonTitles:@"从相册选取",nil];
	actionSheet.actionSheetStyle = UIActionSheetStyleDefault;

    actionSheet.tag = [(smPhotoItemView*)sel tag];//_ACTIONSHEET_PHOTOCHOOSE_;
 	
    [actionSheet showInView:self.view];
}



- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    
    if (buttonIndex == actionSheet.cancelButtonIndex) {
        return;   
    }
    
#if (TARGET_IPHONE_SIMULATOR)
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"模拟器无法模拟照相机和相册" message:@"请在真机上玩"
                                                   delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
    [alert show];	
    return;
#endif

    //选择照片
    if (YES) {//actionSheet.tag == _ACTIONSHEET_PHOTOCHOOSE_) {
        
        self.getPhoto = [[GetPhotos alloc] init];
        [self.getPhoto initWithController:self];
        self.getPhoto.tag = actionSheet.tag;
        [self.getPhoto setDelegate:self];
        
        if (buttonIndex == actionSheet.destructiveButtonIndex) {
            [self.getPhoto getPhotosFromCamera];        
        } else if (buttonIndex == actionSheet.firstOtherButtonIndex) {
            [self.getPhoto getPhotosFromPhotoLibiary];
        }
    }
    
    //地址切换
    //    else if (actionSheet.tag == _ACTIONSHEET_ADDRESSGET_) {
    //        if (buttonIndex == actionSheet.firstOtherButtonIndex) {
    //            [self.myShopAddressLabel setText:[self.locationDateSource currentPosition]];
    //        }
    //    }

}


- (void) getPhotoFinished:(UIImage *)image {
    [[self.myOrderPhontos objectAtIndex:self.getPhoto.tag] setShopNewImage:image];
    
    [self.myScrollView reloadInputViews];
}




@end
